package com.itlong.cloud.property;

import com.itlong.cloud.POJO.DTO.common.PadFaceSyncDataDTO;
import com.itlong.cloud.POJO.VO.property.PropertyArrearageBillGetVO;
import com.itlong.cloud.equip.property.IPadFaceSyncDataServiceClient;
import com.itlong.cloud.iottp.IIotTpJPushClientServiceClient;
import com.itlong.cloud.project.property.IPropertyAccountServiceClient;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      物业-账单管理定时统计未缴费账单滞纳金
 * </desc>
 *
 * @createDate 2018/08/29
 */
@EnableScheduling // 启用定时任务
@RestController
public class PropertyPenaltyStatisticsTimedTask {

    private static Logger LOG = LoggerFactory.getLogger(PropertyPenaltyStatisticsTimedTask.class);

    @Autowired
    private IPropertyAccountServiceClient iPropertyAccountServiceClient;

    @Autowired
    private IIotTpJPushClientServiceClient iIotTpJPushClientServiceClient;

    @Autowired
    IPadFaceSyncDataServiceClient iPadFaceSyncDataServiceClient;

    /**
     * <desc>
     *      定时任务统计滞纳金
     * </desc>
     *
     * @author wangzhi
     * @createDate 2018/08/29
     */
//    @Scheduled(cron = "0 * * * * ?") // 每分钟执行一次（用于测试）
    @Scheduled(cron = "0 20 0 * * ?") // 每天凌晨0点20分执行
    public void statisticsPenaltyTask() throws Exception{
        try {
            List<PropertyArrearageBillGetVO> propertyArrearageBillGetVOList = iPropertyAccountServiceClient.statisticsPenalty();
            if (propertyArrearageBillGetVOList != null && !propertyArrearageBillGetVOList.isEmpty()) {
                List<String> alreadyPushed = new ArrayList<>();
                Map<String,String> comands = new HashMap<>();
                comands.put("refreshSips","1");
                for(PropertyArrearageBillGetVO propertyArrearageBillGetVO : propertyArrearageBillGetVOList){
                    String projectIdAndPhone = propertyArrearageBillGetVO.getProjectId() + "," + propertyArrearageBillGetVO.getPhoneNum();
                    if (!alreadyPushed.contains(projectIdAndPhone)) {
                        iIotTpJPushClientServiceClient.pushWithTaskTO(propertyArrearageBillGetVO.getProjectId(),propertyArrearageBillGetVO.getPhoneNum(),comands,"欠费联动app功能限制变更");
                        alreadyPushed.add(projectIdAndPhone);
                    }
                    PadFaceSyncDataDTO padFaceSyncDataDTO = new PadFaceSyncDataDTO();
                    padFaceSyncDataDTO.setOperateType("0");
                    padFaceSyncDataDTO.setAppUserIds(propertyArrearageBillGetVO.getUserId());
                    padFaceSyncDataDTO.setProjectId(propertyArrearageBillGetVO.getProjectId());
                    iPadFaceSyncDataServiceClient.userOrFloorSync(padFaceSyncDataDTO);
                }
            }
//            LOG.info("【定时任务】定时统计滞纳金成功");
        } catch (Exception e) {
            LOG.error("【定时任务】定时统计滞纳金失败");
            throw e;
        }
    }

}
